package com.wanghlme.day27.service.impl;

import com.wanghlme.day27.dao.IDeptDao;
import com.wanghlme.day27.dao.IEmpDao;
import com.wanghlme.day27.dao.impl.DeptDaoImpl;
import com.wanghlme.day27.dao.impl.EmpDaoImpl;
import com.wanghlme.day27.filter.EncodingFilter;
import com.wanghlme.day27.pojo.Dept;
import com.wanghlme.day27.pojo.Emp;
import com.wanghlme.day27.service.ICompanyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class CompanyServiceImpl implements ICompanyService {
    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());
    private IDeptDao deptDao;
    private IEmpDao empDao;

    public CompanyServiceImpl() {
        deptDao = new DeptDaoImpl();
        empDao = new EmpDaoImpl();
    }

    @Override
    public List<Emp> listAllEmp() {
        List<Emp> empList = empDao.listAllEmp();
        int countEmpList = empList.size();
        if (countEmpList > 0) {
            for (int i = 0; i < countEmpList; i++) {
                Emp emp = empList.get(i);
                Dept dept = deptDao.queryDeptById(emp.getDept().getDeptId());
                emp.setDept(dept);
            }
        }
        return empList;
    }

    @Override
    public List<Dept> listAllDept() {
        List<Dept> deptList = deptDao.listAllDept();
        return deptList;
    }

    @Override
    public Dept queryDeptById(Integer id) {
        return deptDao.queryDeptById(id);
    }


    @Override
    public Emp addEmp(Emp emp) {
        int key = empDao.insertEmp(emp);
        return empDao.queryEmpById(key);
    }

    @Override
    public Integer updateEmpStatus(Integer id, Integer empStatus) {
        return empDao.updateEmpStatus(id, empStatus);
    }

    @Override
    public Emp updateEmp(Emp emp) {
        empDao.updateEmp(emp);
        return empDao.queryEmpById(emp.getEmpId());
    }
}
